package com.yh.csx.crm.core.config;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/*
@Configuration
public class RedisConfig {
    @Value("${redis.nodes}")
    private List<String> nodes;
    @Value("${redis.connectTimeout:2000}")
    private int connectTimeout;
    @Value("${redis.soTimeout:10000}")
    private int soTimeout;
    @Value("${redis.maxAttempts:5}")
    private int maxAttempts;
    @Value("${redis.password:}")
    private String password;


    @Bean
    @ConfigurationProperties("redis.pool")
    public GenericObjectPoolConfig redisPoolConfig() {
        GenericObjectPoolConfig pool = new GenericObjectPoolConfig();
        pool.setMaxIdle(10);
        pool.setMinIdle(1);
        return pool;
    }

    @Bean
    public JedisCluster jedisCluster() {
        if (StringUtils.isBlank(password)) password = null;
        return new JedisCluster(createAddressList(), connectTimeout, soTimeout, maxAttempts, password, redisPoolConfig());
    }

    private Set<HostAndPort> createAddressList() {
        Set<HostAndPort> addressList = new LinkedHashSet<>();
        if (nodes != null) {
            for (String node : nodes) {
                HostAndPort hp;
                try {
                    int index = node.indexOf(':');
                    String ip = node.substring(0, index).trim();
                    Integer port = Integer.valueOf(node.substring(index + 1).trim());
                    hp = new HostAndPort(ip, port);
                } catch (Exception e) {
                    throw new IllegalStateException("[redis.nodes]配置格式错误");
                }
                addressList.add(hp);
            }
        }
        return addressList;
    }
}*/
